package com.vegetable.modules.controller.finance;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.CustomerCollectionAlert;
import com.vegetable.modules.entity.finance.po.CustomerCollectionAlertQueryPO;
import com.vegetable.modules.entity.finance.vo.CustomerCollectionAlertVO;
import com.vegetable.modules.service.finance.ICustomerCollectionAlertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 客户回款预警设置 前端控制器
 * </p>
 *
 * @author vegetable
 * @since 2025-10-24
 */
@Slf4j
@RestController
@RequestMapping("/finance/customerCollectionAlert")
@Api(value = "客户回款预警设置", tags = "客户回款预警设置")
public class CustomerCollectionAlertController extends AbstractController {

    @Autowired
    private ICustomerCollectionAlertService alertService;

    /**
     * 分页查询客户回款预警设置列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询客户回款预警设置列表", notes = "分页查询客户回款预警设置列表")
    public R list(@Valid CustomerCollectionAlertQueryPO queryPO) {
        log.info("分页查询客户回款预警设置列表：{}", JSON.toJSONString(queryPO));
        IPage<CustomerCollectionAlertVO> page = alertService.queryPage(queryPO);
        return R.data(page);
    }

    /**
     * 根据ID查询客户回款预警设置详情
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据ID查询客户回款预警设置详情", notes = "根据ID查询客户回款预警设置详情")
    public R info(@PathVariable("id") Long id) {
        log.info("查询客户回款预警设置详情，ID：{}", id);
        CustomerCollectionAlert alert = alertService.getById(id);
        return R.data(alert);
    }

    /**
     * 根据客户ID查询预警设置
     */
    @GetMapping("/getByCustomerId")
    @ApiOperation(value = "根据客户ID查询预警设置", notes = "根据客户ID查询预警设置")
    public R getByCustomerId(@RequestParam Long customerId, @RequestParam(required = false, defaultValue = "0") Integer regionId) {
        log.info("根据客户ID查询预警设置，客户ID：{}，台账ID：{}", customerId, regionId);
        CustomerCollectionAlert alert = alertService.getByCustomerId(customerId, regionId);
        return R.data(alert);
    }

    /**
     * 新增客户回款预警设置
     */
    @SysLog(value = "新增客户回款预警设置")
    @PostMapping("/save")
    @ApiOperation(value = "新增客户回款预警设置", notes = "新增客户回款预警设置")
    public R save(@RequestBody CustomerCollectionAlert alert) {
        log.info("新增客户回款预警设置：{}", JSON.toJSONString(alert));
        alert.setCreateUserId(getUserId());
        return alertService.saveAlert(alert);
    }

    /**
     * 更新客户回款预警设置
     */
    @SysLog(value = "更新客户回款预警设置")
    @PostMapping("/update")
    @ApiOperation(value = "更新客户回款预警设置", notes = "更新客户回款预警设置")
    public R update(@RequestBody CustomerCollectionAlert alert) {
        log.info("更新客户回款预警设置：{}", JSON.toJSONString(alert));
        return alertService.updateAlert(alert);
    }

    /**
     * 删除客户回款预警设置
     */
    @SysLog(value = "删除客户回款预警设置")
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除客户回款预警设置", notes = "删除客户回款预警设置")
    public R delete(@PathVariable("id") Long id) {
        log.info("删除客户回款预警设置，ID：{}", id);
        return alertService.deleteAlert(id);
    }

    /**
     * 启用/禁用客户回款预警
     */
    @SysLog(value = "启用/禁用客户回款预警")
    @PostMapping("/toggle")
    @ApiOperation(value = "启用/禁用客户回款预警", notes = "启用/禁用客户回款预警")
    public R toggle(@RequestParam Long id, @RequestParam Integer isEnabled) {
        log.info("{}客户回款预警，ID：{}", isEnabled == 1 ? "启用" : "禁用", id);
        return alertService.toggleAlert(id, isEnabled);
    }
}
